<?php 
namespace app\common\model;
class MemberStatistics extends Common{
	protected $insert = ['addtime'=>SYS_TIME];
	protected function getAddtimeAttr($value){
		return date("Y-m-d H:i", $value);
	}
    protected function getAlldataAttr($value, $data){
        $data['realname'] = model('AdminUser')->where('userid', $data['userid'])->value('realname');
        return $data;
    }
    //添加数据
    public function save_data($data){ 
        //allowField过滤post数组中的非数据表字段数据
        $result = $this->allowField(true)->isUpdate(false)->data($data)->save();
        if($result!==false){
            $pk = $this->getPk();
            $id = $this->$pk;
            return $id;
        }
        return false;
    }
	//增加数量
    public function add_log($kind=1, $nums=1, $addtime=SYS_TIME){
        //每天排名
        $nowday = date("Y-m-d",$addtime);
        $count_day = $this->where('kind', $kind)->where('addday', $nowday)->where('typeid', 1)->count();
        if (!$count_day) {
            $data = array(
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 1,
                'addday' => $nowday,
            );
            $this->save_data($data);
        }else{
            $this->where('kind', $kind)->where('addday', $nowday)->where('typeid', 1)->setInc('nums', $nums);
        }
        //每周排名
        $nowweek = date("Y", $addtime)."-".date('W')."-W";
        $count_week = $this->where('kind', $kind)->where('addday', $nowweek)->where('typeid', 2)->count();
        if (!$count_week) {
            $data = array(
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 2,
                'addday' => $nowweek,
            );
            $this->save_data($data);
        }else{
            $this->where('kind', $kind)->where('addday', $nowweek)->where('typeid', 2)->setInc('nums', $nums);
        }

        //每月排名
        $nowmou = date("Y-m",$addtime);
        $count_mou = $this->where('kind', $kind)->where('addday', $nowmou)->where('typeid', 3)->count();
        if (!$count_mou) {
            $data = array(
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 3,
                'addday' => $nowmou,
            );
            $this->save_data($data);
        }else{
            $this->where('kind', $kind)->where('addday', $nowmou)->where('typeid', 3)->setInc('nums', $nums);
        }
        //总排名
        $count = $this->where('kind', $kind)->where('typeid', 4)->count();
        if (!$count) {
            $data = array(
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 4,
                'addday' => date("Y-m-d",SYS_TIME),
            );
            $this->save_data($data);
        }else{
            $this->where('kind', $kind)->where('typeid', 4)->setInc('nums', $nums);
        }
        return true;
    }
    public function get_lists(){
        $monstart = date('Y-m', SYS_TIME).'-01 00:00:00';
        $list = $this->where('addtime', 'gt', $monstart)->where('typeid', 1)->order('id asc')->select();
        $lists = [];
        $ordlist = [];
        $reglist = [];
        $x_axis = [];
        $max = substr(date('Y-m-d', SYS_TIME), -2);
        for ($i=1; $i <= $max; $i++) { 
            $x_axis[] = str_pad((string)$i, 2, '0', STR_PAD_LEFT);
        }
        foreach ($list as $v) {
            if ($v['kind'] == 1) {
                $reglist[] = $v;
            }else{
                $ordlist[] = $v;
            }
        }
        $str = date('Y-m', SYS_TIME);
        $reglist = get_every($reglist, $x_axis, $str.'-');
        $ordlist = get_every($ordlist, $x_axis, $str.'-');
        $lists['nowmon'] = date('Y-m', SYS_TIME);
        $lists['x_axis'] = implode(',', $x_axis);
        $lists['reginfo'] = implode(',', $reglist);
        $lists['ordinfo'] = implode(',', $ordlist);
        //当月
        $nowmou = date("Y-m",SYS_TIME);
        $monlist = $this->list_data(['addday'=>$nowmou, 'typeid'=>3]);
        foreach ($monlist as $v) {
            if ($v['kind'] == 1) {
                $lists['regmon'] = $v['nums'];
            }else{
                $lists['ordmon'] = $v['nums'];
            }
        }
        //总共
        $totallist = $this->list_data(['typeid'=>4]);
        $lists['regtotal'] = 0;
        $lists['ordtotal'] = 0;
        foreach ($totallist as $v) {
            if ($v['kind'] == 1) {
                $lists['regtotal'] = $v['nums'];
            }else{
                $lists['ordtotal'] = $v['nums'];
            }
        }
       return $lists;
    }
}